package codingforgreat.lchot150;

public class Lc042 {
    public int trap(int[] height) {
        int N = height.length;
        int Lmax = height[0];
        int Rmax = height[height.length - 1];
        int L = 1;
        int R = N - 2;
        int ans = 0;
        while(L <= R){
            if(Lmax <= Rmax){
                ans += Math.max(Lmax - height[L],0);
                Lmax = Math.max(Lmax,height[L++]);
            }else{
                ans += Math.max(Rmax - height[R],0);
                Rmax = Math.max(Rmax,height[R--]);
            }
        }
        return ans;
    }
}
